<%@ page import="java.net.URLDecoder" %>
<%@ page import="java.util.Objects" %>
<%@ page pageEncoding="UTF-8" contentType="text/html; UTF-8" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%
    // 获取cookies
    String username = null;
    boolean isAdmin = false;
    Cookie cookie = null;
    Cookie[] cookies = null;
    // 获取cookies的数据,是一个数组
    cookies = request.getCookies();
    if (cookies != null) {

        for (int i = 0; i < cookies.length; i++) {
            cookie = cookies[i];
            if (Objects.equals(cookie.getName(), "username")) {
                username = URLDecoder.decode(cookie.getValue(), "utf-8");
                break;
            }
        }
    }
    if (username == null) {
        response.sendRedirect("/Mall");
    } else {
        if (username.equals("admin")) isAdmin = true;
    }
    pageContext.setAttribute("username", username);
    pageContext.setAttribute("isAdmin", isAdmin);
%>

<nav class="navbar navbar-expand-sm bg-dark navbar-dark fixed-top">
    <!-- logo位 -->
    <a class="navbar-brand" href="#">
        Mall电商运营平台
    </a>
    <!-- 导航位 -->
    <ul class="navbar-nav mr-auto">
        <c:choose>
            <c:when test="${isAdmin}">
                <li class="nav-item">
                    <a class="nav-link" href="../product/DataView.jsp">数据看板</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="${pageContext.request.contextPath}/product/list">商品维护</a>
                </li>

                <li class="nav-item">
                    <a class="nav-link" href="${pageContext.request.contextPath}/user/list">会员管理</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="${pageContext.request.contextPath}/cart/list">购物车管理</a>
                </li>
            </c:when>
        </c:choose>
<%--        <li class="nav-item">--%>
<%--            <a class="nav-link" href="../product/DataView.jsp">数据看板</a>--%>
<%--        </li>--%>
<%--        <li class="nav-item">--%>
<%--            <a class="nav-link" href="${pageContext.request.contextPath}/product/list">商品维护</a>--%>
<%--        </li>--%>

<%--        <li class="nav-item">--%>
<%--            <a class="nav-link" href="${pageContext.request.contextPath}/user/list">会员管理</a>--%>
<%--        </li>--%>
        <li class="nav-item">
            <a class="nav-link" href="${pageContext.request.contextPath}/product/search">商品搜索</a>
        </li>
        <%--<li class="nav-item">
            <a class="nav-link" href="${pageContext.request.contextPath}/cart/list">购物车管理</a>
        </li>--%>
        <li class="nav-item">
            <a class="nav-link" href="${pageContext.request.contextPath}/order/list?username=<%=username%>">订单管理</a>
        </li>
    </ul>
    </ul>
    <!-- 个人中心 -->
    <div style="display: flex;font-size: 14px; font-family: '楷体'; ">
        <span style="" class="navbar-text">[<%=username %>] </span>
        <a class="navbar-text " href="/Mall/cart/findByName?username=<%=username%>">购物车</a>
        <a class="navbar-text " href="/Mall/admin/findByName?username=<%=username%>">个人中心</a>
        <a class="navbar-text " href="#" onclick="bye()">再见</a>
    </div>

</nav>
<script>
    function bye() {
        var expiresDate = new Date(0); // 设置为过去的时间，使cookie立即过期
        var path = "/Mall"; // 根据实际应用的路径设置cookie路径，这里假设为根路径

        if (confirm("确认退出？")) {
            window.location.href = "../index.jsp";

            // 删除username cookie
            document.cookie = "username=" + encodeURIComponent("<%=username%>") + ";expires=" + expiresDate.toUTCString() + ";path=" + path + ';';
        }
    }

    // function deleteCookie(key){
    //
    // }

</script>

